package work.konbai.bmc.cms.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import work.konbai.bmc.cms.domain.BillDO;
import work.konbai.bmc.cms.service.BillService;
import work.konbai.bmc.common.annotation.Log;
import work.konbai.bmc.common.constant.consist.BillType;
import work.konbai.bmc.common.constant.enums.ExpenseStateEnum;
import work.konbai.bmc.common.constant.enums.ExpenseStateEnum.*;
import work.konbai.bmc.common.controller.BaseController;
import work.konbai.bmc.common.utils.PageUtils;
import work.konbai.bmc.common.utils.Query;
import work.konbai.bmc.common.utils.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 账单申请/审批管理
 *
 * @author KonBAI
 * @version 1.0
 * Create by 2020/02/10 22:42
 */
@Controller
@RequestMapping("/cms/expense")
public class ExpenseController extends BaseController {

    private String prefix = "cms/expense";

    @Autowired
    private BillService billService;

    /**
     * 跳转到报销申请页面
     * @return
     */
    @GetMapping("/apply")
    String Apply(){
        return prefix + "/apply";
    }

    /**
     * 查询申请列表
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/apply/list")
    public PageUtils listApply(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        query.put("applyBy", getUserId());
        List<BillDO> billList = billService.list(query);
        int total = billService.count(query);
        PageUtils pageUtils = new PageUtils(billList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    // @RequiresPermissions("cms:bill:add")
    String add(){
        return prefix + "/add";
    }

    /**
     * 申请账单报销
     * @param bill
     * @return
     */
    @Log("申请账单报销")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("cms:expense:apply")
    public R save(BillDO bill){
        bill.setCreateUser(getUser().getName());
        bill.setApplyBy(getUserId());
        bill.setApplyUser(getUser().getName());
        bill.setState(ExpenseStateEnum.REVIEW.getCode());
        bill.setTypeFlag(BillType.EXPENSE);
        bill.setApplyTime(new Date());
        if(billService.save(bill)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 跳转到报销审核页面
     * @return
     */
    @GetMapping("/approval")
    String Approval(){
        return prefix + "/approval";
    }

    /**
     * 查询审核列表
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/approval/list")
    @RequiresPermissions("cms:expense:approval")
    public PageUtils listApproval(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        query.put("typeFlag", BillType.EXPENSE);
        List<String> states = new ArrayList<>();
        states.add(ExpenseStateEnum.AGREE.getCode());
        states.add(ExpenseStateEnum.REFUSE.getCode());
        states.add(ExpenseStateEnum.REVIEW.getCode());
        query.put("states", states);
        List<BillDO> approvalList = billService.list(query);
        int total = billService.count(query);
        PageUtils pageUtils = new PageUtils(approvalList, total);
        return pageUtils;

    }

    /**
     * 修改账单状态
     * @param bill
     * @return
     */
    @Log("修改账单状态")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("cms:expense:update")
    public R updateState(BillDO bill) {
        billService.update(bill);
        return R.ok();
    }
}
